package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.SysWorkTimeSettingDao;
import com.ccp.dev.system.model.SysWorkTime;
import com.ccp.dev.system.model.SysWorkTimeSetting;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * sys_worktime_setting 信息操作服务类
 *
 * @author hw
 * @date 2019-10-15 14:32:52
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysWorkTimeSettingService extends BaseService<SysWorkTimeSetting> {

    @Resource
    private SysWorkTimeSettingDao sysWorkTimeSettingDao;
    @Resource
    private SysWorkTimeService sysWorkTimeService;

    /**
     * 保存班次信息
     * @param sysWorkTimeSetting
     * @return
     */
    public int save(SysWorkTimeSetting sysWorkTimeSetting){
        int result = 0;
        if(StringUtil.isNotEmpty(sysWorkTimeSetting.getId())){
            this.sysWorkTimeService.delBySettingId(sysWorkTimeSetting.getId());
            List<SysWorkTime> sysWorkTimeList = sysWorkTimeSetting.getSysWorkTimeList();
            for(int i=0;i<sysWorkTimeList.size();i++){
                sysWorkTimeList.get(i).setSettingId(sysWorkTimeSetting.getId());
                sysWorkTimeList.get(i).setId(UUIDUtils.getUUIDFor32());
            }
            if(sysWorkTimeSetting.getSysWorkTimeList().size() != 0){
                this.sysWorkTimeService.addList(sysWorkTimeSetting.getSysWorkTimeList());
            }
            result = this.sysWorkTimeSettingDao.update(sysWorkTimeSetting);
        }else{
            String settingId = UUIDUtils.getUUIDFor32();
            sysWorkTimeSetting.setId(settingId);
            List<SysWorkTime> sysWorkTimeList = sysWorkTimeSetting.getSysWorkTimeList();
            for(int i=0;i<sysWorkTimeList.size();i++){
                sysWorkTimeList.get(i).setId(UUIDUtils.getUUIDFor32());
                sysWorkTimeList.get(i).setSettingId(settingId);
            }
            if(sysWorkTimeList.size() > 0){
                this.sysWorkTimeService.addList(sysWorkTimeList);
            }
            result = this.sysWorkTimeSettingDao.add(sysWorkTimeSetting);
        }
        return result;
    }

    public SysWorkTimeSetting geSettingDataById(String id){
        SysWorkTimeSetting sysWorkTimeSetting = this.sysWorkTimeSettingDao.getById(id);
        List<SysWorkTime> sysWorkTimeList = this.sysWorkTimeService.queryListBySettingId(id);
        sysWorkTimeSetting.setSysWorkTimeList(sysWorkTimeList);
        return sysWorkTimeSetting;
    }

}
